using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 寒霜Buff - 降低目标移动速度和攻击速度
/// </summary>
public class ChillBuff : BuffBase
{
    private float m_OriginalSpeed;
    private float m_OriginalAttackSpeed;
    private float m_SpeedReduction;
    private float m_AttackSpeedReduction;

    public ChillBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is ChillData chillData)
        {
            m_SpeedReduction = chillData.SpeedReduction;
            m_AttackSpeedReduction = chillData.AttackSpeedReduction;
        }
        else
        {
            m_SpeedReduction = 0.3f; // 默认减速30%
            m_AttackSpeedReduction = 0.2f; // 默认攻击速度降低20%
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始速度
        if (Target.MoveComponent != null)
        {
            m_OriginalSpeed = Target.MoveComponent.Speed;
            Target.MoveComponent.SetSpeed(m_OriginalSpeed * (1.0f - m_SpeedReduction));
        }
        
        // 保存原始攻击速度（这里需要根据AttrComponent的具体实现）
        // if (Target.AttrComponent != null)
        // {
        //     m_OriginalAttackSpeed = Target.AttrComponent.AttackSpeed;
        //     Target.AttrComponent.SetAttackSpeed(m_OriginalAttackSpeed * (1.0f - m_AttackSpeedReduction));
        // }
        
        // 播放寒霜特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状态
        if (Target.Animator != null)
        {
            Target.Animator.SetFloat("speedMultiplier", 1.0f - m_SpeedReduction);
        }
        
        Logger.Log("ChillBuff", $"{Target.name} 被寒霜影响，移动速度降低 {m_SpeedReduction * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 寒霜期间不需要特殊处理
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("ChillBuff", $"{Target.name} 寒霜效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始速度
        if (Target.MoveComponent != null)
        {
            Target.MoveComponent.SetSpeed(m_OriginalSpeed);
        }
        
        // 恢复原始攻击速度
        // if (Target.AttrComponent != null)
        // {
        //     Target.AttrComponent.SetAttackSpeed(m_OriginalAttackSpeed);
        // }
        
        // 恢复动画状态
        if (Target.Animator != null)
        {
            Target.Animator.SetFloat("speedMultiplier", 1.0f);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Chill"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 寒霜效果可以叠加，刷新持续时间
    }
}